Input/output library
From Cppreference
| C++ Standard Library | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||
| Input/output library | ||||||||||||
Standard input/output library is organized in such a way that the underlying device of an input/output operation is abstracted. In this way the same code can handle input/output to a file, memory stream, or custom adaptor device interpreting the data on the fly, e.g. compressing it. Most of the classes are templated, so they can be adapted to any basic character type. Separate typedefs are provided for the most common basic character types (char and wchar_t).
The library consists of the following elements:
- Abstraction:
- raw device abstraction class:
- classes that act as a wrapper for a given abstract device and provide high level input/output interface:
- basic_istream (input stream)
- basic_ostream (output stream)
- basic_iostream (input/output stream)
- These classes internally inherit several utility classes:
- Implementation:
- file based streams:
- basic_filebuf (raw file device implementation)
- basic_ifstream (input file stream)
- basic_ofstream (output file stream)
- basic_fstream (input-output file stream)
- string based streams:
- basic_stringbuf (raw string device implementation)
- basic_istringstream (input string stream)
- basic_ostringstream (output string stream)
- basic_stringstream (input-output string stream)
Contents |
[edit] Abstractions
[edit] Raw device
The interface with underlying raw device is abstracted by abstract class template basic_streambuf. This class is not meant to be copyable: one basic_streambuf object corresponds to one actual device.
[edit] Device management
Class template basic_ios manages an object that implements basic_streambuf interface. Several basic_ios objects can refer to one actual basic_streambuf object. basic_ios stores error and formatting flags. A lot of this functionality is placed in ios_base base class.
[edit] Streams
The class templates basic_istream, basic_ostream and basic_iostream provide high level input/output capabilities such as sequential reading/writing as well as formatting. These classes all implement that functionality over interface, provided by the basic_streambuf class. It is accessed through basic_ios class, which basic_istream, basic_ostream and basic_iostream inherit. The capabilities of these classes are as follows:
- basic_istream allows input operations
- basic_ostream allows output operations
- basic_iostream allows both input and output operations
[edit] Implementations
[edit] Raw devices
Two classes that implements the basic_streambuf interface are provided:
- class template basic_filebuf implements file based device
- class template basic_filebuf implements memory (cpp/string/basic_string) based device.
[edit] Streams
Three class templates pair basic_istream, basic_ostream and basic_iostream with basic_filebuf:
- class template basic_ifstream provides support for file input operations.
- class template basic_ofstream provides support for file output operations.
- class template basic_fstream provides support for both file input and output operations.
Three class templates pair basic_istream, basic_ostream and basic_iostream with basic_stringbuf:
- class template basic_istringstream provides support for memory-based input operations.
- class template basic_ostringstream provides support for memory-based output operations.
- class template basic_stringstream provides support for both memory-based input and output operations.
[edit] Typedefs
The following typedefs for common character types are provided:
typedef basic_streambuf<char> streambuf; typedef basic_streambuf<wchar_t> wstreambuf; typedef basic_filebuf<char> filebuf; typedef basic_filebuf<wchar_t> wfilebuf; typedef basic_stringbuf<char> stringbuf; typedef basic_stringbuf<wchar_t> wstringbuf; typedef basic_istream<char> istream; typedef basic_istream<wchar_t> wistream; typedef basic_ostream<char> ostream; typedef basic_ostream<wchar_t> wostream; typedef basic_iostream<char> iostream; typedef basic_iostream<wchar_t> wiostream; typedef basic_ifstream<char> ifstream; typedef basic_ifstream<wchar_t> wifstream; typedef basic_ofstream<char> ofstream; typedef basic_ofstream<wchar_t> wofstream; typedef basic_fstream<char> fstream; typedef basic_fstream<wchar_t> wfstream; typedef basic_istringstream<char> istringstream; typedef basic_istringstream<wchar_t> wistringstream; typedef basic_ostringstream<char> ostringstream; typedef basic_ostringstream<wchar_t> wostringstream; typedef basic_stringstream<char> stringstream; typedef basic_stringstream<wchar_t> wstringstream;
Predefined standard stream objects:
extern istream cin; //standard input (stdin) extern wistream wcin; extern ostream cout; //standard output (stdout) extern wostream wcout; extern ostream cerr; //standard error (stderr) extern wostream wcerr; extern ostream clog; //standard log (stdlog) extern wostream wclog;
[edit] Manipulators
Several manipulators are defined to control the formatting of input/output operations on streams.
Some of the behavior of the C++ I/O streams (precision, justification,
etc) may be modified by manipulating various I/O stream
format flags.
|exceptions|set the stream to throw exceptions on errors| |rdstate|returns the state flags of the stream|